package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.entry.YddCate;
import com.itheima.ydd.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {
    @Autowired
    private YddCateService yddCateService;

    /**
     * 分类列表-分页
     *
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R<PageDto<YddCate>> list(String catename, String page, String limit) {
        log.info("page={},limit={},catename={}", page, limit, catename);
        long pageLong = Long.parseLong(page);
        long limitLong = Long.parseLong(limit);
        //自定义分页构造器
        PageDto<YddCate> pageInfo = new PageDto<>(pageLong, limitLong);
        //条件构造器
        LambdaQueryWrapper<YddCate> lambdaQueryWrapper = new LambdaQueryWrapper<YddCate>();
        //过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(catename), YddCate::getCatename, catename);
        //排序
        lambdaQueryWrapper.orderByAsc(YddCate::getSort_num);
        //查询
        yddCateService.page(pageInfo, lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 分类列表-全部
     *
     * @return
     */
    @GetMapping("/all")
    public R<List<YddCate>> all() {
        log.info("分类列表-全部....");
        List<YddCate> list = yddCateService.list();
        return R.success(list);
    }

    /**
     * 分类添加
     *
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddCate yddCate) {
        log.info("yddCate:{}", yddCate);
        String cateName = yddCate.getCatename().trim();
        String icon = yddCate.getIcon().trim();
        if (StringUtils.isNotEmpty(cateName)&&StringUtils.isNotEmpty(icon)) {
            boolean res = yddCateService.save(yddCate);
            return res ? R.success("添加成功") : R.error("添加失败");
        }
        return R.error("添加失败,分类名称和字段图标不能为空");
    }

    /**
     * 分类删除(单个删除)
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> del(String id) {
        log.info("删除分类，id为：{}", id);
        boolean res = yddCateService.removeById(id);
        return res ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 分类详情--修改数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddCate> rowInfo(String id) {
        log.info("根据id查询分类详情，id为：{}", id);
        YddCate yddCate = yddCateService.getById(id);
        if (yddCate != null) {
            return R.success(yddCate);
        }
        return R.error("没有查询到对应信息");
    }

    /**
     * 分类修改
     *
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddCate yddCate) {
        log.info("yddCate:{}", yddCate);
        String catename = yddCate.getCatename().trim();
        String icon = yddCate.getIcon().trim();
        if (StringUtils.isNotEmpty(catename)&&StringUtils.isNotEmpty(icon)) {
            boolean res = yddCateService.updateById(yddCate);
            return res ? R.success("修改成功") : R.error("修改失败");
        }
        return R.error("修改失败,分类名称和字段图标不能为空");
    }
}
